import rpcCreator from './rpc.js'
import { OdooBus } from './rpc.js'

import { ModelClass } from './models'

const get_addons = () => {
  const odooAddons2 = require.context('./addons2', true, /\.js$/)

  const addons2 = odooAddons2.keys().reduce((models, modulePath) => {
    // const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
    const value = odooAddons2(modulePath)
    models = { ...models, ...value.default }
    return models
  }, {})

  const odooAddonsFiles = require.context('./addons', true, /\.js$/)
  // console.log('addons 0', odooAddonsFiles)

  const addons1 = odooAddonsFiles.keys().reduce((models, modulePath) => {
    // set './app.js' => 'app'
    const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
    const value = odooAddonsFiles(modulePath)
    // console.log('addons 1', modulePath, moduleName, value)
    models[moduleName] = value.default
    return models
  }, {})

  const addons = { ...addons1, ...addons2 }

  return addons
}

const addons = get_addons()

// const get_addons_test = () => {
//   const res = require('./addons2/res').default
//   const addons = { ...res }
//   return addons
// }
// const addons = get_addons_test()

console.log('addons, ', addons)

export class ODOO {
  constructor(params) {
    const { error } = params
    this.rpc = rpcCreator({ error })
    this.bus = OdooBus.getBus()
  }

  // Not Used
  ref(xmlid) {
    // get model and id from xmlid
    return this.env('ir.model.data').call('xmlid_to_res_model_res_id', [
      xmlid,
      true
    ])
  }

  get_userinfo() {
    return this.rpc.get_userinfo()
  }

  env(model) {
    const get_actions = model => {
      //
      // 1. model_addons === false, then readonly
      // 2. model_addons === true, model_addons.metadata === false, then all true
      // 3. model_addons === true, model_addons.metadata === true, then model_addons.metadata.actions

      const actions = {
        search: true,
        browse: true,
        create: true,
        write: true,
        unlink: true
      }
      const model_addons = addons[model]
      if (!model_addons) {
        return { ...actions, create: false, write: false, unlink: false }
      } else {
        const { metadata } = model_addons
        const actions_from = (metadata || {}).actions || {}
        return { ...actions, ...actions_from }
      }
    }

    const get_model_config = model => {
      // console.log('env, get_model_config,', model)
      const model_addons = addons[model] || {}
      const {
        _name = model,
        _description,
        _inherit,
        _fields = {},
        metadata = {},
        extend
      } = model_addons

      // TBD 2020-6-28,  actions 没考虑继承
      metadata.actions = get_actions(model)

      if (_inherit) {
        const parent = get_model_config(_inherit)
        // console.log('parent, ', _inherit, parent)

        return {
          _name: parent._name,
          _description: _description || parent._description,
          _fields: {
            ...parent._fields,
            ..._fields
          },

          // TBD 2020-6-28, metadata 子项的 嵌套继承问题
          metadata: { ...parent.metadata, ...metadata },
          MyModelClass: extend
            ? extend(parent.MyModelClass)
            : parent.MyModelClass
        }
      } else {
        return {
          _name,
          _description: _description || model,
          _fields: {
            // display_name: { type: 'char', string: '名称' },
            ..._fields
          },
          metadata,
          MyModelClass: extend ? extend(ModelClass) : ModelClass
        }
      }
    }

    // console.log('env, ', model)
    const model_config = get_model_config(model)
    // console.log('model_config, ', model, model_config)

    return new model_config.MyModelClass({
      model: model_config._name,
      metadata: {
        description: model_config._description,
        fields: model_config._fields,
        ...model_config.metadata
      },
      rpc: this.rpc,
      env: this.env
    })
  }
}

const odooCreator = ({ error }) => {
  return new ODOO({ error })
}

export default odooCreator
